#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;
//输入流运算符重载
class Complex{
public:
    Complex(int real, int image)
    :_real(real)
    ,_image(image)
    {

    }


    friend istream & operator>>(istream & is, Complex & rhs);
    friend ostream & operator<<(ostream & os, const Complex & rhs);
private:
    int _real;
    int _image;
};
//此时的输入流运算符不够健壮
//可以在写一个版本
#if 0
istream & operator>>(istream & is, Complex & rhs){
    cout << "请输入实部" << endl;
    is >> rhs._real;
    cout << "请输入虚部" << endl;
    is >> rhs._image;
    return is;
}
#endif
void readInt(istream & is, int & number){
    while(1){
        is >> number;
        if(is.fail()){
            cout << "当前输入的数字有误，请重新输入" << endl;
            //重置流的状态并且清空缓冲区里面的脏数据
            is.clear();
            is.ignore(1024, '\n');
        }else if(is.bad()){
            cout << "当前流状态有误" << endl;
        }else if(is.good()){
            break;
        }
    }
}

istream & operator>>(istream & is, Complex & rhs){
    cout << "请输入实部" << endl;
    readInt(is, rhs._real);
    cout << "请输入虚部" << endl;
    readInt(is, rhs._image);
    return is;
}

ostream & operator<<(ostream & os, const Complex & rhs){
    os << rhs._real << "+" << rhs._image << "i" << endl;
    return os;
}

void test(){
    Complex c1(1, 2);
    cin >> c1;
    cout << c1 << endl;
}

void test2(){
    int number;
    cin >> number;
    while(cin >> number){
        //cin >> number返回结果是cin对象本身
        //cin对象置于if条件、while语句中会进行隐式类型转换
    }
}

int main()
{
    test();
    return 0;
}

